package com.example.hcbmsm.service.impl;

import com.example.hcbmsm.domain.Drug;
import com.example.hcbmsm.domain.DrugExample;
import com.example.hcbmsm.domain.Inv;
import com.example.hcbmsm.mapper.DrugMapper;
import com.example.hcbmsm.service.DrugService;
import com.example.hcbmsm.service.InvService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/11/1 16:35
 */
@Service
public class DrugServiceImpl implements DrugService {
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private InvService invService;

    @Override
    public Map list() {
        DrugExample drugExample = new DrugExample();
        return HCBMSMResult.SUCCESS(drugMapper.selectByExample(drugExample));
    }

    @Override
    public Drug getDrug(Long drugId) {
        return drugMapper.selectByPrimaryKey(drugId);
    }

    @Override
    public Map listByPage(String drugName, int page, int limit) {
        DrugExample drugExample = new DrugExample();
        Integer total = (int) drugMapper.countByExample(drugExample);
        if (drugName != null && !Objects.equals(drugName, "")){
            drugExample.createCriteria().andDrugNameLike("%" + drugName + "%");
        }
        PageHelper.startPage(page, limit);
        List<Drug> drugs = drugMapper.selectByExample(drugExample);
        Map map = new HashMap();
        map.put("total", total);
        map.put("data", drugs);
        return HCBMSMResult.SUCCESS(map);
    }

    @Override
    public Map add(Drug drug) {
        DrugExample drugExample = new DrugExample();
        drugExample.createCriteria().andDrugNameEqualTo(drug.getDrugName());
        List<Drug> drugs = drugMapper.selectByExample(drugExample);
        if (!drugs.isEmpty() && !Objects.equals(drugs.get(0).getDrugId(), drug.getDrugId())) {
            return HCBMSMResult.WARNING(301, "药品已存在");
        }
        if (drugMapper.insertSelective(drug) == 1) {
            Inv inv = new Inv();
            drug = drugMapper.selectByExample(drugExample).get(0);
            inv.setDrugId(drug.getDrugId());
            inv.setInvAmount(0);
            invService.addInv(inv);
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map renew(Drug drug) {
        DrugExample drugExample = new DrugExample();
        drugExample.createCriteria().andDrugNameEqualTo(drug.getDrugName());
        List<Drug> drugs = drugMapper.selectByExample(drugExample);
        drug.setModifyTime(new Date());
        if (!drugs.isEmpty() && !Objects.equals(drugs.get(0).getDrugId(), drug.getDrugId())) {
            return HCBMSMResult.WARNING(301, "药品已存在");
        }
        if (drugMapper.updateByPrimaryKeySelective(drug) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long drugId) {
        if (drugMapper.deleteByPrimaryKey(drugId) == 1) {
            invService.removeByDrugId(drugId);
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
